<?php
class WotHub extends SmartbokController
{
    protected $_pdo;
    
    public function __construct($requete, $reponse) {
        parent::__construct($requete, $reponse);
        if (isset($_SESSION['account_id']) === true) {
            $this->membre_id = $_SESSION['account_id'];
        } 
        if (isset($_SESSION['accountInfo']['role']) === true) {
            $this->addProfils($_SESSION['accountInfo']['role']);
        }
        $this->_pdo = new smartbokModele($this->bdd);
        $this->_initData();
        $this->_addClanCss();
        $this->_params = $this->getParams();
    }
    
    protected function _addClanCss() {
        if (isset($_SESSION['clanInfo']['abbreviation']) === true) {
            $cssFile = $_SESSION['clanInfo']['abbreviation'].'.css';
            if (file_exists('css/'.$cssFile) === true) {
                $this->ajouterScript('css/'.$cssFile, 'css');
            }
        }
    }
    protected function _buildContexte() {
        if (isset($_SESSION['accountInfo']) === true) {
            if (isset($_SESSION['accountInfo']['clanId']) === true && $_SESSION['accountInfo']['clanId'] !== 0) {
                $clanName = $_SESSION['clanInfo']['name'];
                $clanDevise = $_SESSION['clanInfo']['motto'];
                $clanLogo = $_SESSION['clanInfo']['emblems']['large'] ;
                $this->_reponse->changeAppVar('appName', $clanName, 'application');
                $this->_reponse->changeAppVar('description', $clanDevise, 'application');
                $this->clanLogo = $clanLogo;
                $this->clanInfo = $_SESSION['clanInfo'];
                $this->_getDataClan($this->clanInfo['clan_id']);
            }
            else {
                $this->clanInfo = null;
                $_SESSION['clanInfo']['name'] = 'Wothub';
            }
            $this->_construireInfoAgenda();
        }
    }
    
    protected function _initData() {
        $account_id = $this->membre_id;
        $appId = $this->appId;
        $this->_model_clan = new Model_Clan($this->_pdo, $appId);
        $this->_model_membre = new Model_Membre($this->_pdo, $appId);
    
        $dataMembre = Model_Membre::getMembreInfo($account_id);
        
        $dataMembreClan = $this->_model_clan->getMembreClanInfo($account_id);
        
        if ($dataMembreClan !== null) {
            $this->_clanId = $dataMembreClan['clan_id'];
            $this->_role = $dataMembreClan['role'];
            $dataClan = $this->_model_clan->getClanInfo($this->_clanId);
            $paramClan = $this->_model_clan->getClanParam($this->_clanId);
            $_SESSION['accountInfo']['role'] = $this->_role;
            $_SESSION['accountInfo']['pseudo'] = $dataMembre['nickname'];
            $_SESSION['accountInfo']['clanId'] = $this->_clanId;
            $_SESSION['accountInfo']['data'] = $dataMembre;
            $_SESSION['clanInfo'] = $dataClan;
            $_SESSION['clanInfo']['params'] = $paramClan[0];
            $_SESSION['accountInfo']['strateur'] = $this->_model_membre->isStrateur($this->_clanId, $this->membre_id);
            $_SESSION['accountInfo']['officier'] = $this->_model_membre->isOfficier($this->_role);
            $this->clanStats = Model_Clan::getClanStat($this->_clanId);
            $this->personnalStats = Model_Membre::getPersonnalRating($account_id);
            $_SESSION['clan'] = true;
        }
        else {
            $_SESSION['accountInfo']['strateur'] = 'ko';
            $_SESSION['accountInfo']['officier'] = 'ko';
            $_SESSION['clanInfo']['params'] = $this->_model_clan->getClanParam(null)[0];
            $_SESSION['clanInfo']['clan_id'] = null; 
            $_SESSION['accountInfo']['clanId'] = 0;
            $_SESSION['accountInfo']['pseudo'] = $dataMembre['nickname'];
            $this->statAccueil = Model_WotHub::getStatAccueil();
            $this->agenda = Model_WotHub::getOpenAgenda();
        }
        $this->_buildContexte();
    }
    
    protected function _construireInfoAgenda() {
        $Events = array();
        $agenda = new Model_Agenda($this->_pdo, $this->appId);
        $actions = $agenda->getAgendaAction($this->baseUri, false);
        $listeEvent = $agenda->getLastEvent();
        $inscriptions = $agenda->getMyInscription();
        foreach ($listeEvent as $date => $event) {
            foreach($event as $key => &$detail) {
                $detail['status'] = '';
                $detail['imageStatus'] = '';
                if (key_exists($key, $inscriptions)) {
                    $detail['status'] = $inscriptions[$key];
                    $detail['imageStatus'] = 'images/'.$actions[$inscriptions[$key]]['image'];
                }
                $detail['liens'] = $this->_construireLiensActionAgenda($actions, $key);
            }
            $Events[$this->_formatDate($date)] = $event;
        }
        $this->agendaGauche = $Events;
    }
    
    protected function _construireLiensActionAgenda($actions, $agenda_id) {
        $liens = array();
        foreach ($actions as $action) {
            $lien = null;
            switch ($action['action_id']) {
                case 4:
                case 5:
                    //on fait rien 
                break;
                default:
                    $lien['libelle'] = $action['libelle'];
                    $url = str_replace('#EVENTID#', $agenda_id, $action['url']);
                    $url = str_replace('#ACTIONID#', $action['action_id'], $url);
                    $lien['url'] = $url;
                break;
            }
            $liens[] = $lien;
        }
        return array_filter($liens);
    }
    
    protected function _formatDate($date) {
        $time = mktime(0, 0, 0, substr($date, 4, 2), substr($date, 6, 2), substr($date, 0 ,4));
        $dateF = strftime('%A %d %B %Y', $time);
        return $dateF;
    }
    
    protected function _getDataClan($clanId) {
        $url = '';
        if (isset($_SESSION['clanInfo']['forum']) !== true || $_SESSION['clanInfo']['forum'] === '') {
            if (isset($this->_pdo) === false) {
                $this->_pdo = new smartbokModele($this->bdd);
            }
            $sql = "SELECT url_forum, nombre_event, creation_team, creation_event FROM clan WHERE clan_id = :clanid";
            $clan = $this->_pdo->executerSql($sql, array(':clanid' => $clanId));
            if (isset($clan[0]) === true) {
                $_SESSION['clanInfo']['forum'] = $clan[0]['url_forum'];
                $_SESSION['clanInfo']['params']['nbEvent'] = $clan[0]['nombre_event'];
                $_SESSION['clanInfo']['params']['creationTeam'] = $clan[0]['creation_team'];
                $_SESSION['clanInfo']['params']['creationEvent'] = $clan[0]['creation_event'];
            }else {                
                $_SESSION['clanInfo']['forum'] = '';
            }
        }
    }
}
/*
http://api.worldoftanks.eu/wot/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500029623
http://api.worldoftanks.eu/wot/clan/membersinfo/?application_id=d0a293dc77667c9328783d489c8cef73&member_id=502732411
http://api.worldoftanks.eu/wot/account/info/?application_id=d0a293dc77667c9328783d489c8cef73&account_id=502732411
*/